
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import biz_mysql
import excel_info_sql
list1 = excel_info_sql.getConditions()

def Inovel_test():
    driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
    driver.get("http://new.emeixs.com/index.php/Admin/Main/index.html")
    driver.maximize_window()
    driver.implicitly_wait(10)


    # 定位账号
    driver.find_element_by_name('username').clear()
    driver.find_element_by_name('username').send_keys('{0}'.format(""))

    # 定位密码
    time.sleep(0.5)
    driver.find_element_by_name('password').clear()
    driver.find_element_by_name('password').send_keys('{0}'.format(''))

    # 点击登录
    time.sleep(6)
    driver.find_element_by_xpath('//a[@class="button button-block button-rounded button-royal button-large"]').click()

    #推广中心
    try:
        WebDriverWait(driver,15).until(
            EC.element_to_be_clickable((By.XPATH,'//*[@id="mCSB_1_container"]//li[3]/a'))
        )
        driver.find_element_by_xpath('//*[@id="mCSB_1_container"]//li[3]/a').click()
    except Exception:
        print("推广中心")


    for i in range(0, len(list1)):
        # 小说名字
        Novel_name = list1[i]["Novel_name"]
        # print(Novel_name)
        # 日期
        insert_time = list1[i]["date_time"]
        # print(insert_time)
        # 章节
        Chapter = list1[i]["Chapter"]
        # print(Chapter)
        # 推广名称
        Extension_name = list1[i]["Extension_name"]
        # print(Extension_name)
        # 平台名称
        Platform = list1[i]["Platform"]

        # 搜索kuang
        try:
            # WebDriverWait(driver,15).until(
            #     EC.element_to_be_clickable((By.XPATH,'//*[@id="keyword"]'))
            # )
            time.sleep(2)
            driver.find_element_by_xpath('//*[@id="keyword"]').clear()
            driver.find_element_by_xpath('//*[@id="keyword"]').send_keys(Novel_name)
        except Exception:
            print("搜索kuang")

        #搜素

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="btn_search"]'))
            )
            driver.find_element_by_xpath('//*[@id="btn_search"]').click()
        except Exception:
            print("搜素")

        #文案推广

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//td[6]/div[2]/a'))
            )
            driver.find_element_by_xpath('//td[6]/div[2]/a').click()
        except Exception:
            print("文案推广")

        # 获得打开的第一个窗口句柄
        window_1 = driver.current_window_handle
        # print(window_1)
        # 获得打开的所有的窗口句柄
        windows = driver.window_handles
        # print(windows)
        # 切换到最新的窗口
        for current_window in windows:
            if current_window != window_1:
                driver.switch_to.window(current_window)

        #推广链接

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//tr[1]/td[2]/a'))
            )
            driver.find_element_by_xpath('//tr[1]/td[2]/a').click()
        except Exception:
            print("推广链接")

        #生成推广链接

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="r_in_link"]/button'))
            )
            driver.find_element_by_xpath('//*[@id="r_in_link"]/button').click()
        except Exception:
            print("生成推广链接")

        #渠道名称

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="pen_name"]'))
            )
            driver.find_element_by_xpath('//*[@id="pen_name"]').clear()
            driver.find_element_by_xpath('//*[@id="pen_name"]').send_keys(insert_time+Extension_name)
        except Exception:
            print("渠道名称")

        #确定

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="btn_new_send"]'))
            )
            driver.find_element_by_xpath('//*[@id="btn_new_send"]').click()
        except Exception:
            print("确定失败")

        #跳转到图片文案

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="btn-img-url"]'))
            )
            driver.find_element_by_xpath('//*[@id="btn-img-url"]').click()
        except Exception:
            print("跳转到图片文案")

        #复制文章内容

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="btn-copy-content"]'))
            )
            driver.find_element_by_xpath('//*[@id="btn-copy-content"]').click()
        except Exception:
            print("复制文章内容")

        #复制原文链接

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="btn-copy-url"]'))
            )
            driver.find_element_by_xpath('//*[@id="btn-copy-url"]').click()
        except Exception:
            print("复制原文链接")

        #获取url

        try:
            time.sleep(1)
            url = driver.execute_script("return jQuery('[id=txt-referral-url]').val();")
            print(url)
            # 数据入库
            biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,datetime=insert_time, Extension_name=insert_time+Extension_name)
        except Exception:
            print("获取url")

        driver.close()
        driver.switch_to.window(window_1)
    driver.quit()

if __name__ == '__main__':
    Inovel_test()








